package com.app.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.app.mapper.*;
import com.app.model.*;
import com.app.service.IntegralGoodsService;
import com.app.utils.CommonHelp;
import com.app.vo.IntegralGoodsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 积分商品
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@Service
public class IntegralGoodsServiceImpl extends ServiceImpl<IntegralGoodsMapper, IntegralGoodsModel> implements IntegralGoodsService {

    @Autowired
    private IntegralGoodsMapper integralGoodsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IntegralOrderMapper integralOrderMapper;
    @Autowired
    private IntegralRecordMapper integralRecordMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    public ProvinceMapper provinceMapper;

    @Override
    public IntegralGoodsVo getIntegralGoodsList(Integer page, Integer limit, AdminUserModel user,Integer areaId,String keyword) {
        Page<IntegralGoodsModel> page1 = new Page<>(page, limit);
        QueryWrapper<IntegralGoodsModel> qw = new QueryWrapper<>();
        if (user.getRoleId() == 3) {
            qw.eq("city_id",areaId).or().eq("city_id", user.getAreaId());
        }
        if (user.getRoleId() == 4) {
            qw.eq("area_id",areaId).or().eq("area_id", user.getAreaId());
        }
        qw.eq("status",1);
        qw.like("name",keyword);
        qw.orderByDesc("create_time");
        Page<IntegralGoodsModel> integralGoodsPage = integralGoodsMapper.selectPage(page1, qw);
        List<IntegralGoodsModel> integralGoodsList = integralGoodsPage.getRecords();
        long count = integralGoodsPage.getTotal();
        IntegralGoodsVo integralGoodsVo = new IntegralGoodsVo();
        integralGoodsVo.setList(integralGoodsList);
        integralGoodsVo.setCount((int) count);
        return integralGoodsVo;
    }
    @Override
    public IntegralGoodsVo getIntegralGoodsList(Integer page, Integer limit, HttpServletRequest request) {
        Page<IntegralGoodsModel> page1 = new Page<>(page, limit);
        QueryWrapper<IntegralGoodsModel> qw = new QueryWrapper<>();
        String cityId = request.getHeader("cityId");
        String areaId = request.getHeader("areaId");
        qw.eq(StrUtil.isNotBlank(areaId),"area_id",areaId);
        qw.eq(StrUtil.isNotBlank(cityId),"city_id",cityId);
        qw.eq("status",1);
        qw.orderByDesc("create_time");
        Page<IntegralGoodsModel> integralGoodsPage = integralGoodsMapper.selectPage(page1, qw);
        List<IntegralGoodsModel> integralGoodsList = integralGoodsPage.getRecords();
        for (IntegralGoodsModel integralGoodsModel : integralGoodsList) {
            //构建areaText
            String areaText;
            String businessAreaId = integralGoodsModel.getAreaId();
            QueryWrapper<AreaModel> areaQW = new QueryWrapper<>();
            areaQW.eq("area_id", businessAreaId);
            AreaModel areaModel = areaMapper.selectOne(areaQW);
            areaText = areaModel.getArea();
            if (integralGoodsModel.getCityId() != null) {
                CityModel cityModel = cityMapper.selectOne(new QueryWrapper<CityModel>()
                        .eq("city_id", integralGoodsModel.getCityId()));
                String city = cityModel.getCity();

                ProvinceModel provinceModel = provinceMapper.selectOne(new QueryWrapper<ProvinceModel>()
                        .eq("province_id", cityModel.getProvinceId()));

                areaText = provinceModel.getProvince() +"\\" +city+"\\"+areaText;
            }
            integralGoodsModel.setAreaText(areaText);
        }
        long count = integralGoodsPage.getTotal();
        IntegralGoodsVo integralGoodsVo = new IntegralGoodsVo();
        integralGoodsVo.setList(integralGoodsList);
        integralGoodsVo.setCount((int) count);
        return integralGoodsVo;
    }

    //兑换积分商品
    @Override
    public String exchangeGoods(IntegralGoodsModel goodsModel, Integer userId) {
        IntegralGoodsModel integralGoodsModel = integralGoodsMapper.selectById(goodsModel.getId());
        if (integralGoodsModel.getNumber() < goodsModel.getNumber()) {
            return "此积分商品库存不足";
        }
        //将用户积分扣除
        UserModel userModel = userMapper.selectById(userId);
        Double userIntegral = userModel.getIntegral();
        Double integral = integralGoodsModel.getIntegral() * goodsModel.getNumber();
        if (userIntegral < integral) {
            return "用户积分不足";
        }
        userIntegral = userIntegral - integral;
        userModel.setIntegral(userIntegral);
        userMapper.updateById(userModel);
        //将积分商品库存扣除
        Integer integralGoodsNumber = integralGoodsModel.getNumber();
        integralGoodsNumber = integralGoodsNumber - goodsModel.getNumber();
        integralGoodsModel.setNumber(integralGoodsNumber);
        integralGoodsMapper.updateById(integralGoodsModel);
        //生成积分订单记录
        IntegralOrderModel integralOrderModel = new IntegralOrderModel();
        integralOrderModel.setIntegral(integralGoodsModel.getIntegral());
        integralOrderModel.setGoodsName(integralGoodsModel.getName());
        integralOrderModel.setGoodsFace(integralGoodsModel.getGoodsFace());
        integralOrderModel.setNumber(goodsModel.getNumber());
        integralOrderModel.setAddressId(goodsModel.getAddressId());
        integralOrderModel.setUserId(userId);
        integralOrderModel.setAreaId(userModel.getAreaId());
        integralOrderModel.setCityId(userModel.getCityId());
        integralOrderModel.setIntegralGoodsId(integralGoodsModel.getId());
        integralOrderModel.setOrderSn(IdUtil.fastSimpleUUID());
        integralOrderModel.setCreateTime(CommonHelp.getNowTimestamp());
        integralOrderMapper.insert(integralOrderModel);
        String orderId = integralOrderModel.getOrderSn();
        //生成用户积分使用记录
        IntegralRecordModel integralRecordModel = new IntegralRecordModel();
        integralRecordModel.setName("积分商城");
        integralRecordModel.setContent(integralGoodsModel.getName());
        integralRecordModel.setTypeName("兑换");
        String amountIntegral = String.format("-%.2f分", integralGoodsModel.getIntegral());
        integralRecordModel.setIntegral(amountIntegral);
        integralRecordModel.setUserId(userId);
        integralRecordModel.setCreateTime(CommonHelp.getNowTimestamp());
        integralRecordModel.setOrderId(orderId);
        integralRecordModel.setVoucherType(0);
        integralRecordModel.setVoucherAmount(-integralGoodsModel.getIntegral());
        integralRecordMapper.insert(integralRecordModel);
        return "兑换成功";
    }
}
